package com.sdonkey.score.service.impl;


import com.sdonkey.score.bean.UpdatePlaceAdForm;
import com.sdonkey.score.dao.ITadPlacementDao;
import com.sdonkey.score.dao.ITadvertiserDao;
import com.sdonkey.score.model.*;
import com.sdonkey.score.service.IAdPlacementService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 已投放广告service
 * @author Administrator
 *
 */
@Service("adPlacementService")
@Transactional
public class AdPlacementService implements IAdPlacementService {
	
	/**
	 * 已投放广告
	 */
	@Override
	public int updateAdStatus(FinishPlacementAdBean finishplacement) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Autowired
	private ITadPlacementDao adPlacementDao;
	
	@Autowired
	private ITadvertiserDao advertiserDao;
    /**
     * 根据广告主id查询已投放广告
     */
	@Override
	public List<FinishPlacementAdBean> getPlacementAds(String advId) {
		return adPlacementDao.getPlacementAds(advId);
	}
	/**
     *  根据广告id查询已投放 广告下面的渠道
     */
	@Override
	public List<TadPlacement> getPlacementById(String advId, int adId) {
		return adPlacementDao.getPlacementById(advId, adId);
	}
	/**
     *  根据渠道id查询渠道
     */
	@Override
	public TadPlacement getPlacementByid(String advId, int adId) {
		return adPlacementDao.getPlacementByPlancementid(advId, adId);
	}
	/**
     * 根据广告主id查询已投放广告名称,id,图片路径
     */
	@Override
	public List<Tad> getPlacementAdsByAdvId(String advId, String type) {
		return adPlacementDao.getPlacementAdsByAdvId(advId,type);
	}
	/**
     * 根据广告主id查出广告主所在的公司和公司图片路径
     */
	@Override
	public TadvertiserNew getCompanyByAdvId(int advId) {
		return adPlacementDao.getCompanyByAdvId(advId);
	}
	/**
     * 查出所有渠道
     */
	@Override
	public List<TadApprovider> getAppprovider() {
		return adPlacementDao.getAppprovider();
	}
//	 /**
//     * 根据广告id查询已投放广告
//     */
//	@Override
//	public EditPlaceAdBean getEditPlaceAdBean(String adId) {
//		return adPlacementDao.getEditPlaceAdBean(adId);
//	}
	 /**
     * 查询用户余额
     */
	@Override
	public Double getBalanceByAdvId(String advId) {
		return adPlacementDao.getBalance(advId);
	}
	/**
	 * 查询单条渠道广告余额
	 *
	 */
	@Override
	public Double getAdvertiserBalanceById(int id) {
		return adPlacementDao.getAdvertiserBalance(id);
	}
	/**
	 * 根据广告id查询下面的渠道   判断是否还有投放的渠道 
	 *
	 */
	@Override
	public List<FinishPlacementAdBean> getPlacementByadId(String advId, String adId) {
		return adPlacementDao.getPlacementByAdId(advId, adId);
	}
	/**
	 * 根据广告id查询下面的渠道id（处于投放中）
	 *
	 */
	@Override
	public List<FinishPlacementAdBean> getPlIdadId(String advId, String adId,String status) {
		return adPlacementDao.getPlIdByAdId(advId, adId, status);
	}
	/**
     * 添加图片类型广告和一系列业务处理
     */
	/*@Override
	@Transactional(propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
	public int savePlaceAd(TadPlacement tadPlacement,String advId,String status,int adId,String money,String balance,TadBehavioralStatistics tadBehavioralStatistics) {
		int priceCheck = 1;
		if(StringUtils.isBlank(money))
			money = "0";
		if(StringUtils.isBlank(balance))
			balance = "0";
		double num = Double.parseDouble(money);
		double actualBalance = Double.parseDouble(balance);
		actualBalance = actualBalance - num;
		try {
//			Double balance = placmentBalance - money;
			int placeAd = adPlacementDao.savePlaceAd(tadPlacement); 
//			System.out.println(money+"------------------------------");
			int cash = advertiserDao.updateActualBalance(advId, num,actualBalance);
			int adPlacmentstatus = adPlacementDao.updateAdPlacmentstatus(status, adId);
			if(!"是".equals(tadBehavioralStatistics.getAdId())){
				int insertBehavioral = adPlacementDao.insertBehavioral(tadBehavioralStatistics);
			}
		} catch (Exception e) {
			priceCheck = 0;
			throw new RuntimeException("添加失败");
		}
		return priceCheck;
	}*/
	/**
     * 添加图片类型广告
     */
	@Override
	public int savePlacementAd(TadPlacement tadPlacement, String advId, String status, int adId) {
		int priceCheck = 1;
		try {
//			Double balance = placmentBalance - money;
			int placeAd = adPlacementDao.savePlaceAd(tadPlacement); 
//			System.out.println(money+"------------------------------");
			int adPlacmentstatus = adPlacementDao.updateAdPlacmentstatus(status, adId);
		} catch (Exception e) {
			priceCheck = 0;
			throw new RuntimeException("添加失败");
		}
		return priceCheck;
	}
	/**
	 * 根据渠道id更新 日期日期广告总金额广告余额广告单价(渠道表)
	 * 
	 * @return
	 */
	@Override
	public int EditPlacment(TadPlacement tadPlacement) {
		return adPlacementDao.updatePlacment(tadPlacement);
	}
	/**
     * 根据广告id修改长传广告的状态
     */
	@Override
	public int EditAdPlacmentstatus(String status, int adId) {
		int adPlacmentstatus = adPlacementDao.updateAdPlacmentstatus(status, adId);
		return adPlacmentstatus;
	}
	/**
	 * 根据渠道id更新 投放和暂停状态(1 投放 2 暂停 )
	 * 
	 * @return
	 */
	@Override
	public int EditPlacmentstatus(String status, int id) {
		return adPlacementDao.updatePlacmentstatus(id, status);
	}
	/**
	 * 保存t_ad_behavioral_statistics 一条数据 
	 * 
	 * @return
	 */
	@Override
	public int saveAdBehavioral(TadBehavioralStatistics tadBehavioralStatistics) {
		return adPlacementDao.insertBehavioral(tadBehavioralStatistics);
	}
	@Override
	public List<ChannelBean> getChannels() {
		return adPlacementDao.getChannels();
	}



//	@Override
//	public int updateAdPlacementStatus(String adId, int status) {
//		return  adPlacementDao.updateAdPlacementStatus(adId,status);		
//	}
	@Override
	public String checkAdBehavioral(String adId) {
		String adId2 = adPlacementDao.checkBehavioral(adId);
		return adPlacementDao.checkBehavioral(adId);
	}

	@Override
	public int savePlaceAd(TadPlacement tadPlacement, String advId, String status, int adId, String money, String balance, TadBehavioralStatistics tadBehavioralStatistics) {
		return 0;
	}

}
